// 查询银行卡归属地
function queryCardRegion() {
	var options = {
		"bankNumber" : document.getElementById("cardID").value
	};
	var para = JSON.stringify(options);
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/bankcardname",
		data : para,
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			var data = result.data
			if (data.status) {
				// 查询到的数据
				/*
				 * "data":{ "bankNumber":"6212262201023557228",
				 * "bankName":"工商银行", "promptMessage":"查询成功！" }
				 */
				document.getElementById("cardName").value = data.bankName
			} else {
				alert(data.promptMessage);
			}
		}
	});
}
// 校验价格
function rollOutValidPrice() {
	document.getElementById("rollPriceError").innerText = "";
	var value = document.getElementById("rollOutPrice").value;
	if (!priceValidator.test(value)) {
		document.getElementById("rollPriceError").innerText = "请输入正确的转出金额";
		return false;
	}
}
// 金额转出
function rollOut() {
	var options = {
		"accountNumber" : localStorage.accountNum, // 账号
		"token" : localStorage.token, // token
		"bankNumber" : document.getElementById("cardID").value, // 银行卡号
		"bankName" : document.getElementById("cardName").value, // 所属银行
		"amount" : document.getElementById("rollOutPrice").value,
		"userName" : document.getElementById("userName").value,
		"phone" : document.getElementById("phone").value,
	};
	var para = JSON.stringify(options);
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/transferout",
		data : para,
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			var data = result.data
			if (data.status) {
				// 查询到的数据
				/*
				 * "data":{ "bankNumber":"6212262201023557228",
				 * "bankName":"工商银行", "promptMessage":"查询成功！" }
				 */
				alert("转出成功，两小时内到账！");
				queryRollHistory();
				queryUserInfo("child");
			} else {
				alert(data.promptMessage);
			}
		}
	});
}

var historyOrder = [];
var currentPage = 1;
var totalPage = 1;
// 查询转出交易记录
function queryRollHistory() {
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token
	};
	$.ajax({
				type : "POST",
				url : "/ThreeNewBoard/queryserial",
				data : JSON.stringify(options),
				crossDomain : true,
				async : true,
				contentType : "application/json",
				dataType : "json",
				success : function(result) {
					if (result.message == "SUCCESS") {
						// data为请求到的数据
						var data = result.data;
						historyOrder = data;
						totalPage = Math.ceil(data.length / 10);
						if (totalPage === 0) {
							var noData = "<div style='width:100%;height:30px;margin:40px 0;text-align: center;'>暂无表格数据</div>";
							document.getElementById("rollNoData").innerHTML = noData;
						}
						document.getElementById("rollTotal").innerText = "";
						$("#rollTotal").append(totalPage);
						dealData();
					} else {
						alert("数据请求错误！");
					}
				}
			});
}

// 处理交易记录表格数据展示
function dealData() {
	var displayOrder = [];
	$("#rollDisplayData").find("tr").remove();
	if (totalPage < 1 || currentPage === totalPage) {
		$("#rollNextPageBtn").attr("disabled", true);
	} else {
		$("#rollNextPageBtn").attr("disabled", false);
	}
	if (currentPage === 1) {
		$("#rollPrePageBtn").attr("disabled", true);
	} else {
		$("#rollPrePageBtn").attr("disabled", false);
	}
	document.getElementById("rollCurrentPage").innerText = currentPage;
	for (var i = (currentPage - 1) * 10; i < currentPage * 10; i++) {
		var item = historyOrder[i];
		if (!item) {
			return;
		}
		displayOrder.push(item);
		row = document.createElement("tr"); // 创建行 
		td1 = document.createElement("td"); // 单号
		td2 = document.createElement("td"); // 数量
		td3 = document.createElement("td"); // 单价
		td4 = document.createElement("td"); // 账户
		td5 = document.createElement("td"); // 价格
		td6 = document.createElement("td"); // 状态
		td7 = document.createElement("td"); // 证券号
		td8 = document.createElement("td"); // 证券名称
		td1.appendChild(document.createTextNode(item.serialNumber));
		td2.appendChild(document.createTextNode(item.flowDirection));
		td3.appendChild(document.createTextNode(item.amount));
		td4.appendChild(document.createTextNode(item.transactionType));
		td5.appendChild(document.createTextNode(item.operatingTime));
		td6.appendChild(document.createTextNode(item.statusInformation));
		td7.appendChild(document.createTextNode(item.userName));
		td8.appendChild(document.createTextNode(item.customerCode));
		row.appendChild(td1);
		row.appendChild(td2);
		row.appendChild(td3);
		row.appendChild(td4);
		row.appendChild(td5);
		row.appendChild(td6);
		row.appendChild(td7);
		row.appendChild(td8);
		$("#rollDisplayData").append(row);

	}
}
// 上一页
function prePage() {
	currentPage--;
	dealData();
}
// 下一页
function nextPage() {
	currentPage++;
	dealData();
}